package com.sls.api.util;

import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

import org.apache.log4j.Logger;

import com.sls.api.game.constant.Constant;
import com.sls.api.game.servlet.SuperServlet;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;

/**
 * 同步定时器
 * 
 * @author Administrator
 * 
 */
public class SynchronizeTimer extends SuperServlet {

	private static final long serialVersionUID = -1868058577893172477L;

	private static Timer timer = null;
	private static Logger logger = Logger.getLogger(SynchronizeTimer.class);

	public static void main(String[] args) {
		startTimer();
	}

	/**
	 * 启动定时器
	 */
	public static void startTimer() {
		timer = new Timer();
		String syncDay = PropertiesUtil.getValue("sync.days");
		String relayDay = PropertiesUtil.getValue("relay.days");
		int days = 0;
		int relayDays = 0;
		try {
			days = Integer.parseInt(syncDay);
			relayDays = Integer.parseInt(relayDay);
		} catch (NumberFormatException e) {
			days = 3;
			relayDays = 1;
		}
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				SocketDTO dto = new SynchronizeTimer().send();
				if (dto != null && dto.getHead() != null
						&& "".equals(dto.getHead().getResponseCode())) {
				} else {
					logger.error(dto.getHead().getResponseMessage());
				}
			}
		}, relayDays * 24 * 3600 * 1000, days * 24 * 3600 * 1000);
		logger.info("定时器启动完成...");
	}

	/**
	 * 发送请求
	 * 
	 * @return
	 */
	public SocketDTO send() {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constant.NUTS_NET_DEATAIL_SYNC);
		head.setIp(PropertiesUtil.getValue("local.ip"));
		head.setUserId("0");
		SocketDTO dto = super.send(head);
		return dto;
	}

}
